// pages/history/history.js
Page({
  data: {
    details:[],
  },
  onLoad: function(options) {
    var id = options.id;
    wx.request({
      url: `http://localhost:2023/public/showhistory/${id}`,
      method:"GET",
      success: res=> {
        console.log(res.data)
        this.setData({
            details:res.data
          })
      },
      fail: err=> {
        console.log(err)
      },
    })
    this.setData({
      currentId: id
    });

    // 读取所有点赞缓存状态
    var cache = wx.getStorageSync('cache_key');
    if (cache) {
      var currentCache = cache[id];
      console.log(id)
      this.setData({
      collection: currentCache
      })
    } else {
      var cache = {};
      cache[id] = false;
      wx.setStorageSync('cache_key',cache);
    }
  },

  // 点击喜爱
  toCollect: function(event) {
    var cache = wx.getStorageSync('cache_key');
    // console.log(cache)
    // 是否喜爱
    var currentCache = cache[this.data.currentId];
    console.log(this.data.currentId)
    currentCache = !currentCache;
    cache[this.data.currentId] = currentCache;
    // 重新缓存
    wx.setStorageSync('cache_key',cache);
    // 更新
    this.setData({
      collection: currentCache
    });
    wx.showToast({
      title: currentCache?'喜爱':'取消',
      icon: 'success',
      duration: 1500
    });
    
  },
})